package com.song.emp.provider.controller;

import com.song.emp.provider.domain.Dept;
import com.song.emp.provider.domain.Emp;
import com.song.emp.provider.service.DeptService;
import com.song.emp.provider.service.EmpService;
import com.song.emp.vo.EmpVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class EmpController {
    @Resource
    private DeptService deptService;
    @Resource
    private EmpService empService;

    /**
     * 员工和部门的连接查询列表
     * @return
     */
    @RequestMapping(value = "/emps",method = RequestMethod.GET)
    public List<EmpVo> findEmps(){
        return this.deptService.findEmps();
    }

    /**
     * 员工的新增和保存
     * @param empVo
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public boolean save(@RequestBody EmpVo empVo){
        try {
            Emp emp=new Emp();
            emp.setAge(empVo.getAge());
            emp.setName(empVo.getName());
            emp.setDId(empVo.getDid());
            if(empVo.getEid()!=null){
                  //修改时
                  emp.setEid(empVo.getEid());
            }
            this.empService.saveOrUpdate(emp);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  false;
    }

    /**
     * 根据eid删除员工
     * @param eid
     * @return
     */
    @RequestMapping(value = "/del/{eid}",method = RequestMethod.GET)
    public boolean del(@PathVariable("eid") int eid){
        try {
            this.empService.removeById(eid);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据eid查询单个员工对象（表连接查询）
     * @param eid
     * @return
     */
    @RequestMapping(value = "/find/{eid}",method = RequestMethod.GET)
    public EmpVo findById(@PathVariable("eid") int eid){
         return this.empService.findById(eid);
    }

    /**
     * 部门列表（用于添加时的部门下拉框）
     * @return
     */
    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    public List<EmpVo> depts(){
        return this.deptService.findDepts();
    }
}
